package com.handinfo.android.ui.window;

import java.util.regex.Pattern;

import android.graphics.Bitmap;
import android.widget.Toast;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.game.IconManger;
import com.handinfo.android.ui.IUIWindows;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.uicontrols.controls.DWBackground;
import com.handinfo.android.uicontrols.controls.DWButton;
import com.handinfo.android.uicontrols.controls.DWFrame;
import com.handinfo.android.uicontrols.controls.DWImageBox;
import com.handinfo.android.uicontrols.controls.DWInputBox;
import com.handinfo.android.uicontrols.controls.DWTextbox;
import com.handinfo.android.uicontrols.controls.DWTitle;
import com.handinfo.android.utils.Tools;

public class UIShopBuy implements IUIWindows{

	private boolean flag = false;
	private DWFrame m_buy_frame = null;
	private DWTitle m_buy_title = null;
	private DWBackground m_buy_background = null;
	private Bitmap m_bm_background = null;
	private DWImageBox m_ib_close = null;
	private Bitmap m_bm_close = null;
	private DWTextbox[] m_buy_textBox = new DWTextbox[7];
	private byte m_textBox_name = 0;
	private byte m_textBox_tishi = 1;
	private byte m_textBox_bangding = 2;
	private byte m_textBox_shuoming = 3;
	private byte m_textBox_shuliang = 4;
	private byte m_textBox_zongjia = 5;
	private byte m_textBox_yuanbao = 6;
	private DWImageBox m_imageBox_left = null;
	private DWImageBox m_imageBox_right = null;
	private Bitmap m_bm_left = null;
	private Bitmap m_bm_right = null;
	private DWInputBox m_buy_intputBox = null;
	private DWButton m_buy_button = null;
	private Bitmap m_button_back = null;
	private Bitmap m_button_anxia = null;
	private int m_num = 1;
	
	private DWImageBox m_buy_picture = null;
	private Bitmap m_shiyitu = null;
	
	private void shopBuy(){
		m_buy_frame = new DWFrame(400,300);
		m_buy_frame.setClickClose(false);
		m_buy_title = new DWTitle("购买", m_buy_frame);
		m_buy_background = new DWBackground(380, 250);
		m_buy_background.setFill(true);
		m_buy_background.setNearAnchor(m_buy_frame, Tools.LEFT_TOP, Tools.LEFT_TOP, 10, 30);
		m_ib_close = new DWImageBox(m_bm_close);
		m_ib_close.addListener(m_close);
		m_ib_close.setNearAnchor(m_buy_title,Tools.RIGHT_TOP , Tools.RIGHT_TOP, 0, 0);
		
		//图标
		m_buy_picture = new DWImageBox(m_shiyitu);
		m_buy_picture.setNearAnchor(m_buy_background, Tools.LEFT_TOP, Tools.LEFT_TOP, 10, 10);
		//名称
		m_buy_textBox[m_textBox_name] = new DWTextbox("神行符");
		m_buy_textBox[m_textBox_name].setNearAnchor(m_buy_picture, Tools.RIGHT_TOP, Tools.RIGHT_TOP, 70, 0);		
		//友情提示
		m_buy_textBox[m_textBox_tishi] = new DWTextbox("还可以买3个");
		m_buy_textBox[m_textBox_tishi].setNearAnchor(m_buy_textBox[m_textBox_name], Tools.RIGHT_TOP, Tools.RIGHT_TOP, 130, 0);
		//绑定状态
		m_buy_textBox[m_textBox_bangding] = new DWTextbox("装备后绑定");
		m_buy_textBox[m_textBox_bangding].setNearAnchor(m_buy_textBox[m_textBox_name], Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM, 0, 30);
		//使用效果
		m_buy_textBox[m_textBox_shuoming] = new DWTextbox("获得双倍经验和双倍金钱");
		m_buy_textBox[m_textBox_shuoming].setNearAnchor(m_buy_picture, Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM, 10, 30);
		//购买数量
		m_buy_textBox[m_textBox_shuliang] = new DWTextbox("购买数量");
		m_buy_textBox[m_textBox_shuliang].setNearAnchor(m_buy_textBox[m_textBox_shuoming], Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM, 0, 40);
		//左减
		m_imageBox_left = new DWImageBox(m_bm_left);
		m_imageBox_left.addListener(m_left);
		m_imageBox_left.setNearAnchor(m_buy_textBox[m_textBox_shuliang], Tools.RIGHT_TOP, Tools.RIGHT_TOP, 50, 0);
		//数量
		m_buy_intputBox = new DWInputBox(80, m_buy_textBox[m_textBox_shuliang].getShowHeight(), m_buy_frame);
		m_buy_intputBox.setText(""+m_num);
		m_buy_intputBox.addListener(m_inputBox);
		m_buy_intputBox.setNearAnchor(m_imageBox_left, Tools.RIGHT_TOP, Tools.RIGHT_TOP, 120, 0);
		//右加
		m_imageBox_right = new DWImageBox(m_bm_right);
		m_imageBox_right.addListener(m_right);
		m_imageBox_right.setNearAnchor(m_buy_intputBox, Tools.RIGHT_TOP, Tools.RIGHT_TOP, 30, 0);

		//购买总价
		m_buy_textBox[m_textBox_zongjia] = new DWTextbox("购买总价："+8888+"元宝");
		m_buy_textBox[m_textBox_zongjia].setNearAnchor(m_buy_textBox[m_textBox_shuliang], Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM, 0, 40);
		m_buy_textBox[m_textBox_yuanbao] = new DWTextbox("当前拥有："+99999+"元宝");
		m_buy_textBox[m_textBox_yuanbao].setNearAnchor(m_buy_textBox[m_textBox_zongjia], Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM, 0, 30);
		//确认购买
		m_buy_button = new DWButton("购买", m_button_back);
		m_buy_button.setDownImage(m_button_anxia);
		m_buy_button.addListener(m_buyButton);
		m_buy_button.setNearAnchor(m_buy_background, Tools.RIGHT_BOTTOM, Tools.RIGHT_BOTTOM, -5, -10);
		
		m_buy_frame.addControl(m_buy_background);
		m_buy_frame.addControl(m_buy_title);
		m_buy_frame.addControl(m_ib_close);
		for(int i = 0;i < 7;i++){
			m_buy_frame.addControl(m_buy_textBox[i]);
		}
		m_buy_frame.addControl(m_buy_picture);
		m_buy_frame.addControl(m_imageBox_left);
		m_buy_frame.addControl(m_imageBox_right);
		m_buy_frame.addControl(m_buy_intputBox);
		m_buy_frame.addControl(m_buy_button);
		DWControlsManager.getInstance().addControl(m_buy_frame);
	}
	
	 public static boolean isNumber(String str){
		  Pattern pattern = Pattern.compile("[0-9]*");
		  Tools.debugPrintln("aaaaaaaaaaaa"+pattern.matcher(str).matches());
		  if(pattern.matcher(str).matches() == false){
			  DWGameManager.getInstance().showToast("请输入数字", Toast.LENGTH_SHORT);
		  }
		  return pattern.matcher(str).matches();
		 }
	 //关闭窗口
	private DWListener m_close = new DWListener() {
		
		public void OnDoubleClick() {
		}
		
		public void OnClick() {
			UIWindows.getInstance().m_shopBuy.close((byte)0);
		}
	};
	//购买
	private DWListener m_buyButton = new DWListener() {
		
		public void OnDoubleClick() {
		}
		
		public void OnClick() {
			if(isNumber(m_buy_intputBox.getText().toString()) ==false){
				m_buy_intputBox.setText(""+m_num);
			}else{
				//发送数据给服务器
				DWGameManager.getInstance().showToast("功能尚未开放", Toast.LENGTH_SHORT);
			}
		}
	};
	//inputBox
	private DWListener m_inputBox = new DWListener() {
		
		public void OnDoubleClick() {
		}
		
		public void OnClick() {		
			Tools.debugPrintln("--------------"+m_buy_intputBox.getText());
			if(isNumber(m_buy_intputBox.getText().toString()) == true){
				m_num = Integer.parseInt(m_buy_intputBox.getText().toString());			
				m_buy_intputBox.setText(""+m_num);
			}
		}
	};
	//减少
	private DWListener m_left = new DWListener() {
		
		public void OnDoubleClick() {
		}
		
		public void OnClick() {
			if(m_num == 1){
				return;
			}else{
				m_num--;
				m_buy_intputBox.setText(""+m_num);
			}
			
		}
	};
	//增加
	private DWListener m_right = new DWListener() {

		public void OnDoubleClick() {
		}

		public void OnClick() {
			Tools.debugPrintln("------3--------"+m_buy_intputBox.getText());
			if (m_num == 100) {
				return;
			} else {
				m_num++;
				m_buy_intputBox.setText(""+m_num);
				Tools.debugPrintln("----m_num-------"+m_num);
			}

		}
	};
	
	public void init() {
//		m_bm_background = UIWindows.createImage("/img/newui/wodeduiwubk_1.gnp");
		m_bm_close = UIWindows.createImage("/img/newui/fanhuianniu_1.gnp");
		m_bm_left = UIWindows.createImage("/img/newui/haoyoufyan_2.gnp");
		m_bm_right = UIWindows.createImage("/img/newui/haoyoufyan_1.gnp");
		m_button_back = UIWindows.createImage("/img/newui/anniu_2.gnp");
		m_button_anxia = UIWindows.createImage("/img/newui/anniu_2ax.gnp");
		
		m_shiyitu = IconManger.getInstance().getIcon(69);
		
		
	}

	public void open(byte index) {
		if(flag == false){
			shopBuy();
			flag=true;
		}else{
			return;
		}
	}

	public void close(byte index) {
		flag=false;
		DWControlsManager.getInstance().removeControl(m_buy_frame);
	}

	public void dispose() {
		
	}

	public void logic() {
	
	}

}
